// 主题配置管理
import colors from "./color.js";

// 主题配置
const themes = {
  light: {
    name: "浅色主题",
    colors: {
      ...colors,
      // 浅色主题特定颜色
      bgPrimary: "#FFFFFF",
      bgSecondary: "#F8F8F8",
      textPrimary: "#222222",
      textSecondary: "#666666",
    },
  },
  dark: {
    name: "深色主题",
    colors: {
      ...colors,
      // 深色主题特定颜色
      bgPrimary: "#1A1A1A",
      bgSecondary: "#2D2D2D",
      textPrimary: "#FFFFFF",
      textSecondary: "#CCCCCC",
      bg1: "#1A1A1A",
      bg2: "#2D2D2D",
      bg3: "#333333",
      bg4: "#404040",
      text1: "#FFFFFF",
      text2: "#CCCCCC",
      text3: "#999999",
      text4: "#666666",
      border1: "#404040",
      border2: "#505050",
      border3: "#606060",
    },
  },
  pet: {
    name: "宠物主题",
    colors: {
      ...colors,
      // 宠物主题特定颜色
      primary: "#FF6B9D",
      secondary: "#FFB6C1",
      accent: "#87CEEB",
      pet1: "#FFB6C1",
      pet2: "#87CEEB",
      pet3: "#DDA0DD",
      pet4: "#F0E68C",
    },
  },
};

// 主题管理类
class ThemeManager {
  constructor() {
    this.currentTheme = "light";
    this.init();
  }

  // 初始化主题
  init() {
    const savedTheme = wx.getStorageSync("theme") || "light";
    this.setTheme(savedTheme);
  }

  // 设置主题
  setTheme(themeName) {
    if (!themes[themeName]) {
      console.warn(`主题 ${themeName} 不存在`);
      return;
    }

    this.currentTheme = themeName;
    wx.setStorageSync("theme", themeName);

    // 更新全局数据
    const app = getApp();
    if (app && app.globalData) {
      app.globalData.currentTheme = themeName;
      app.globalData.themeColors = themes[themeName].colors;
    }

    // 触发主题变化事件
    this.triggerThemeChange();
  }

  // 获取当前主题
  getCurrentTheme() {
    return themes[this.currentTheme];
  }

  // 获取当前主题颜色
  getCurrentColors() {
    return themes[this.currentTheme].colors;
  }

  // 获取所有主题
  getAllThemes() {
    return Object.keys(themes).map((key) => ({
      key,
      ...themes[key],
    }));
  }

  // 切换主题
  toggleTheme() {
    const themeKeys = Object.keys(themes);
    const currentIndex = themeKeys.indexOf(this.currentTheme);
    const nextIndex = (currentIndex + 1) % themeKeys.length;
    this.setTheme(themeKeys[nextIndex]);
  }

  // 触发主题变化事件
  triggerThemeChange() {
    // 可以通过事件总线通知其他组件主题变化
    if (typeof this.onThemeChange === "function") {
      this.onThemeChange(this.currentTheme);
    }
  }

  // 注册主题变化监听器
  onThemeChange(callback) {
    this.onThemeChange = callback;
  }
}

// 创建全局主题管理器实例
const themeManager = new ThemeManager();

export default themeManager;
export { themes };
